<template>
	<view class="frame">
		<view class="f_top">
			<u-textarea v-model="value1" border='none' count placeholder="请填写问题描述以便我们提供更好的帮助" maxlength="200" autoHeight
				ref="textarea" @input="handleInput"></u-textarea>
		</view>
		<view class="f_bottom">
			<view class="fb_top">
				提供问题截图
			</view>
			<view class="fb_content">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="4"></u-upload>
			</view>
		</view>

		<view class="list" @tap="routerTo('/pages/page/me/feedback/feedbackList')">反馈记录</view>
		<button class="button" color='#2578F4' @click="submit" :disabled="disabled">提交反馈</button>
	</view>
</template>

<script>
	import config from '@/common/config.js';
	export default {
		data() {
			return {
				fileList1: [],
				value1: '',
				disabled: false
			}
		},
		onLoad() {

		},
		onReady() {
			// 如果需要兼容微信小程序的话，需要用此写法
			this.$refs.textarea.setFormatter((value) => {
				if (value.length >= 200) {
					return value.substr(0, 200)
				} else {
					return value
				}
			})
		},
		methods: {
			handleInput(event) {
				const inputValue = event;
				const length = this.getActualLength(inputValue);
				if (length > 200) {
					// 截取前 200 个有效字符
					this.value1 = this.truncateToLength(inputValue, 200);
				} else {
					this.value1 = inputValue;
				}
			},
			getActualLength(str) {
				// 使用 Intl.Segmenter 来计算有效字符长度
				if (typeof Intl.Segmenter !== 'undefined') {
					const segmenter = new Intl.Segmenter();
					return [...segmenter.segment(str)].length;
				} else {
					// 兼容性处理
					let count = 0;
					for (let i = 0; i < str.length; i++) {
						const code = str.charCodeAt(i);
						if ((code >= 0xd800 && code <= 0xdbff) && i + 1 < str.length) {
							// 处理代理对
							i++;
						}
						count++;
					}
					return count;
				}
			},
			truncateToLength(str, length) {
				if (typeof Intl.Segmenter !== 'undefined') {
					const segmenter = new Intl.Segmenter();
					const segments = [...segmenter.segment(str)];
					return segments.slice(0, length).map(segment => segment.segment).join('');
				} else {
					let result = '';
					let count = 0;
					for (let i = 0; i < str.length; i++) {
						const code = str.charCodeAt(i);
						if ((code >= 0xd800 && code <= 0xdbff) && i + 1 < str.length) {
							// 处理代理对
							if (count + 1 < length) {
								result += str[i] + str[i + 1];
								count++;
								i++;
							} else {
								break;
							}
						} else {
							if (count < length) {
								result += str[i];
								count++;
							} else {
								break;
							}
						}
					}
					return result;
				}
			},
			routerTo(e) {
				uni.navigateTo({
					url: e
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: config.configURL + 'common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							Authorization: 'Bearer ' + uni.getStorageSync('token')
						},
						success: (res) => {
							let res2 = JSON.parse(res.data)
							setTimeout(() => {
								resolve(res2.fileName)
							}, 1000)
						}
					});
				})
			},
			submit() {
				var taht = this
				if (!taht.value1) {
					uni.showToast({
						title: '请输入反馈内容',
						icon: 'none',
						duration: 2000
					})
				} else if (taht.fileList1.length == 0) {
					uni.showToast({
						title: '请上传反馈图片',
						icon: 'none',
						duration: 2000
					})
				} else{
					taht.disabled = true
					let img = taht.fileList1.map(res => {
						return res.url
					})
					var agasftetf = img.map(item => item).join(',');
					taht.$api.getfeedback({
						question: taht.value1,
						questionImg: agasftetf
					}).then(res => {
						uni.redirectTo({
							url: '/pages/page/me/feedback/feedbackSuccess'
						})
						taht.disabled = false
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.frame {
		width: 100%;
		min-height: 100vh;

		// ::v-deep .u-upload__wrap {
		// 	view {
		// 		border: 2rpx dashed rgba(211, 213, 218, 1.0) !important;
		// 		border-radius: 10rpx;
		// 	}
		// }

		.f_top {
			width: 95%;
			//styleName: H4 28rpx (Regular) 文本内容、控件文字;
			font-family: 'Alibaba PuHuiTi 2.0';
			font-size: 28rpx;
			font-weight: 400;
			line-height: 40rpx;
			letter-spacing: 0.04em;
			text-align: left;
			padding: 20rpx;
			box-sizing: border-box;
			background: rgba(245, 246, 248, 1);
			border-radius: 20rpx;
			margin: 20rpx auto;

			::v-deep .u-textarea {
				background: rgba(245, 246, 248, 1) !important;
			}

			::v-deep .u-textarea__count {
				bottom: -22rpx;
				background: rgba(245, 246, 248, 1) !important;
			}
		}

		.f_bottom {
			width: 95%;
			margin: 24rpx auto 0;
			background: rgba(245, 246, 248, 1);
			//styleName: H4 28rpx (Regular) 文本内容、控件文字;
			font-family: 'Alibaba PuHuiTi 2.0';
			font-size: 28rpx;
			font-weight: 400;
			line-height: 40rpx;
			letter-spacing: 0.04em;
			text-align: left;
			border-radius: 20rpx;
			padding: 20rpx;
			box-sizing: border-box;

			.fb_top {
				margin-bottom: 20rpx;
			}
		}

		.list {
			width: 90%;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			margin: 52rpx auto 20rpx;
			color: #0CCEA1;
			font-size: 30rpx;
			border-radius: 16rpx;
			position: fixed;
			bottom: 194rpx;
			left: 5%;
		}

		.button {
			width: 90%;
			height: 80rpx;
			line-height: 80rpx;
			margin: 52rpx auto 20rpx;
			background-color: #0CCEA1;
			font-size: 30rpx;
			color: white;
			border-radius: 300rpx;
			position: fixed;
			bottom: 102rpx;
			left: 5%;
		}
	}
</style>